package com.iflytek.universitymanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iflytek.universitymanagement.config.Result;
import com.iflytek.universitymanagement.domain.SurveyQuestion;
import com.iflytek.universitymanagement.service.SurveyQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;


@RestController
@RequestMapping("/api/question")
@Controller
public class SurveyQuestionController {

    
    private static final Logger logger = Logger.getLogger(SurveyQuestionController.class.getName());

        private final SurveyQuestionService surveyQuestionService;

        @Autowired
        public SurveyQuestionController(SurveyQuestionService surveyQuestionService) {
            this.surveyQuestionService = surveyQuestionService;
        }

        /**
         * 获取所有心理健康测试题目
         *
         * @return JSON 格式的题目列表
         */
        @GetMapping("/getAllQuestions")
        public Map<String, Object> getAllQuestions() {
            Map<String, Object> response = new HashMap<>();
            try {
                List<SurveyQuestion> questions = surveyQuestionService.list();  // 获取题目列表
                if (questions.isEmpty()) {
                    response.put("code", 404);
                    response.put("message", "没有找到题目数据");
                    logger.warning("没有找到题目数据");
                } else {
                    response.put("code", 200);
                    response.put("message", "success");
                    response.put("items", questions);

                    // 输出数据库中的题目信息到控制台
                    logger.info("成功返回题目数据: " + questions.size() + "条记录");

                    // 逐个输出每个题目的详细信息
                    for (SurveyQuestion question : questions) {
                        logger.info("题目ID: " + question.getTid() + ", 题目内容: " + question.getDescription());
                    }
                }
            } catch (Exception e) {
                response.put("code", 500);
                response.put("message", "服务器内部错误");
                logger.severe("获取题目时发生错误: " + e.getMessage());
            }
            return response;
        }


        /**
         * 新增
         */
        @PostMapping("/add")
        public Result add(@RequestBody SurveyQuestion surveyQuestion) {
            surveyQuestionService.add(surveyQuestion);
            return Result.success();
        }

        /**
         * 删除
         */
        @DeleteMapping("/delete/{id}")
        public Result deleteById(@PathVariable Integer id) {
            surveyQuestionService.removeById(id);
            return Result.success();
        }

        /**
         * 批量删除
         */
        @DeleteMapping("/delete/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) {
            surveyQuestionService.removeByIds(ids);
            return Result.success();
        }

        /**
         * 修改
         */
        @PutMapping("/update")
        public Result updateById(@RequestBody SurveyQuestion surveyQuestion) {
            surveyQuestionService.updateById(surveyQuestion);
            return Result.success();
        }



        /**
         * 分页查询
         */
        @GetMapping("/selectPage")
        public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
            QueryWrapper<SurveyQuestion> queryWrapper = new QueryWrapper<SurveyQuestion>().orderByAsc("tid");
            Page<SurveyQuestion> page = surveyQuestionService.page(new Page<>(pageNum, pageSize), queryWrapper);
            return Result.success(page);
        }

}



